'use strict';

const { Controller } = require('egg');
const moment = require('moment');

class HomeController extends Controller {
    async update() {
        const { ctx,app } = this;
        const { 
            h_company_name,
            h_company_describe,
            h_banner,
            h_company_wechatqr,
            h_illustration,
            h_company_phone,
            h_company_email,
            h_company_addr,
            h_company_wechat,
            h_company_qq,
            h_homeid
        } = ctx.request.body;

        const Home = await ctx.model.Home.findAll();
        console.log(Home)
        let res = null;
        if(Home.length == 0){
            const create_time = moment().format("YYYY-MM-DD HH:mm");
            const update_time = moment().format("YYYY-MM-DD HH:mm");
            const result = await ctx.model.Home.create({
                h_company_name,
                h_company_describe,
                h_banner,
                h_company_wechatqr,
                h_illustration,
                h_company_phone,
                h_company_email,
                h_company_addr,
                h_company_qq,
                h_company_wechat
            });
            res = Home;
        }else{
            const update_time = moment().format("YYYY-MM-DD HH:mm");
            const result = await this.ctx.model.Home.update({
                h_company_name,
                h_company_describe,
                h_banner,
                h_company_wechatqr,
                h_illustration,
                h_company_phone,
                h_company_email,
                h_company_addr,
                h_company_qq,
                h_company_wechat
            }, { where: { h_homeid:h_homeid } })
            res = result;
        }
        ctx.body = {
            data: res,
            code: '1',
            msg:"更新成功"
        }
        //   if(user){

        //   }
    }

    async getInfo(){
        const { ctx,app } = this;
        const Home = await ctx.model.Home.findAll();
        ctx.body = {
            data: Home,
            code: '1',
            msg:"查询成功"
        }
    }

    async addBanner(){
        const { ctx,app } = this;
        const { 
            title,
            banner_describe,
            img,
            sn
        } = ctx.request.body;
        const create_time = moment().format("YYYY-MM-DD HH:mm");
        const update_time = moment().format("YYYY-MM-DD HH:mm");
        const imgFile = await this.ctx.model.Files.findAll({ where: { h_filesid: img } })
        console.log(imgFile,'imgFile')
        let imgUrl = "";
        if(imgFile){
            imgUrl = imgFile[0].dataValues.h_file_url.replace(/\\/g, "/");
        }
        
        const result = await ctx.model.Banner.create({
            title,
            banner_describe,
            img,
            sn,
            create_time,
            update_time,
            imgUrl
        });
        let res = result;
        ctx.body = {
            data:res,
            code: '1',
            msg:"新增成功"
        }
    }

    async updateBanner(){
        const { ctx,app } = this;
        const {
            h_bannerid, 
            title,
            sn,
            banner_describe,
            img
        } = ctx.request.body;
        const update_time = moment().format("YYYY-MM-DD HH:mm");
        const imgFile = await this.ctx.model.Files.findAll({ where: { h_filesid: img } })
        console.log(imgFile,'imgFile')
        let imgUrl = "";
        if(imgFile){
            imgUrl = imgFile[0].dataValues.h_file_url.replace(/\\/g, "/");
        }
        const result = await this.ctx.model.Banner.update({
            title,
            banner_describe,
            img,
            sn,
            update_time,
            imgUrl
        }, { where: { h_bannerid:h_bannerid } })
        let res = result;
        ctx.body = {
            data:res,
            code: '1',
            msg:"更新成功"
        }
    }

    async removeBanner(){
        const { ctx,app } = this;
        const {
            h_bannerid
        } = ctx.request.body;
        const result = await this.ctx.model.Banner.destroy({ where: { h_bannerid: h_bannerid } });
        ctx.body = {
            data:result,
            code: '1',
            msg:"删除成功"
        }
    }

    async getBanner(){
        const { ctx,app } = this;
        const result = await this.ctx.model.Banner.findAll({
            order:[
                ['sn','ASC']
            ]
        });
        ctx.body = {
            data:result,
            code: '1',
            msg:"查询成功"
        }
    }
}

module.exports = HomeController;